class MyLinkedList {
    int val;
    MyLinkedList next;
    MyLinkedList head;
    public MyLinkedList() {
    }
    public int get(int index) {
        if(index < 0) return -1;
        MyLinkedList cur = head;
        while(cur != null && index > 0) {
            cur = cur.next;
            index--;
        }
        return cur == null ? -1 : cur.val;
    }

    public void addAtHead(int val) {
        if(head == null) {
            head = new MyLinkedList();
            head.val = val;
            return;
        }
        MyLinkedList newHead = new MyLinkedList();
        newHead.val = val;
        newHead.next = head;
        head = newHead;
    }

    public void addAtTail(int val) {
        if(head == null) {
            head = new MyLinkedList();
            head.val = val;
            return;
        }
        MyLinkedList cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = new MyLinkedList();
        cur.next.val = val;
    }

    public void addAtIndex(int index, int val) {
        if(index < 0 || head == null) return;
        MyLinkedList cur = head;
        while(cur != null && index - 1 > 0) {
            cur = cur.next;
            index--;
        }
        if(cur == null) return;
        MyLinkedList temp = new MyLinkedList();
        temp.val = val;
        temp.next = cur.next;
        cur.next = temp;
    }

    public void deleteAtIndex(int index) {
        if(index < 0 || head == null) return;
        if(index == 0) {
            head = head.next;
            return;
        }
        MyLinkedList cur = head;
        while(cur != null && index - 1 > 0) {
            cur = cur.next;
            index--;
        }
        if(cur == null || cur.next == null) return;
        cur.next = cur.next.next;
    }
}